//===============================================================================
// Microsoft patterns & practices
// Smart Client Software Factory 2010
//===============================================================================
// Copyright (c) Microsoft Corporation.  All rights reserved.
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY
// OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT
// LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
// FITNESS FOR A PARTICULAR PURPOSE.
//===============================================================================
// The example companies, organizations, products, domain names,
// e-mail addresses, logos, people, places, and events depicted
// herein are fictitious.  No association with any real company,
// organization, product, domain name, email address, logo, person,
// places, or events is intended or should be inferred.
//===============================================================================
//----------------------------------------------------------------------------------------
// patterns & practices - Smart Client Software Factory - Guidance Package
//
// This file was generated by the "Add View" recipe.
//
// This class is the concrete implementation of a View in the Model-View-Presenter 
// pattern. Communication between the Presenter and this class is acheived through 
// an interface to facilitate separation and testability.
// Note that the Presenter generated by the same recipe, will automatically be created
// by CAB through [CreateNew] and bidirectional references will be added.
//
// For more information see:
// ms-help://MS.VSCC.v90/MS.VSIPCC.v90/ms.practices.scsf.2008apr/SCSF/html/02-09-010-ModelViewPresenter_MVP.htm
//
// Latest version of this Guidance Package: http://go.microsoft.com/fwlink/?LinkId=62182
//----------------------------------------------------------------------------------------

using System;
using System.Windows.Forms;
using Microsoft.Practices.CompositeUI.SmartParts;
using Microsoft.Practices.ObjectBuilder;
using QuickStart.Infrastructure.Interface;
using System.Collections.Generic;
using QuickStart.RestaurantModule.Properties;
using System.Globalization;

namespace QuickStart.RestaurantModule
{
    public partial class MenuItemsView : UserControl, IMenuItemsView
    {
        public MenuItemsView()
        {
            InitializeComponent();
        }

        protected override void OnLoad(EventArgs e)
        {
            _presenter.OnViewReady();
            base.OnLoad(e);
        }

        #region IMenuItemsView Members

        private delegate void ShowMenuItemsDelegate(string restaurantName, IEnumerable<QuickStart.RestaurantModule.RestaurantService.MenuItem> menuItems);
        public void ShowMenuItems(string restaurantName, IEnumerable<QuickStart.RestaurantModule.RestaurantService.MenuItem> menuItems)
        {
            if (InvokeRequired)
            {
                Invoke(new ShowMenuItemsDelegate(ShowMenuItems), restaurantName, menuItems);
            }
            else
            {
                _errorLabel.Visible = false;
                _retryLabel.Visible = false;
                _menuItemsListView.Visible = true;
                _offlineLabel.Visible = false;

                _menuItemsListView.Items.Clear();
                _menuItemsListView.Groups["menuGroup"].Header = String.Format(CultureInfo.CurrentCulture, Resources.MenuItemsHeader, restaurantName);
                foreach (QuickStart.RestaurantModule.RestaurantService.MenuItem menuItem in menuItems)
                {
                    ListViewItem item = new ListViewItem(menuItem.Name, _menuItemsListView.Groups["menuGroup"]);
                    _menuItemsListView.Items.Add(item);
                }
            }
        }


        private delegate void ShowConnectionErrorDelegate();
        public void ShowConnectionError()
        {
            if (InvokeRequired)
            {
                Invoke(new ShowConnectionErrorDelegate(ShowConnectionError));
            }
            else
            {
                _retryLabel.Visible = true;
                _errorLabel.Text = Resources.ConnectionErrorMessage;
                _errorLabel.Visible = true;
                _menuItemsListView.Visible = false;
                _offlineLabel.Visible = false;
            }
        }

        private delegate void ShowOfflineMessageDelegate();
        public void ShowOfflineMessage()
        {
            if (InvokeRequired)
            {
                Invoke(new ShowOfflineMessageDelegate(ShowOfflineMessage));
            }
            else
            {
                _menuItemsListView.Visible = false;
                _offlineLabel.Visible = true;
                _errorLabel.Visible = false;
                _retryLabel.Visible = false;
            }
        }

        #endregion

        private void _retryLabel_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            _presenter.TryGetMenuItems();
        }
    }
}

